from mysql_connect import col_name as col
from mysql_connect.mysql_db import MysqlDB
from logs.user_log import UserLog
from dataconfig.file_config import FilePath
from util.operation_json import OperetionJson
import json
my_db = MysqlDB()
log = UserLog().get_log()
path = FilePath()
op_json = OperetionJson()

class GetData(object):

    def get_value_by_ColName(self, case_dict, col_name):
        value = case_dict[col_name]
        print(value)
        return value

    def get_data_list(self, case_dict):
        sql_id = case_dict[col.sql_id]
        case_id = case_dict[col.case_id]
        case_name = case_dict[col.case_name]
        url = case_dict[col.url]
        is_run = case_dict[col.is_run]
        cookie = case_dict[col.cookie]
        token = case_dict[col.token]
        header_key = case_dict[col.header]
        header_type = case_dict[col.header_type]
        method = case_dict[col.method]
        data_key = case_dict[col.data]
        pre_case_id = case_dict[col.pre_case_id]
        pre_case_key = case_dict[col.pre_case_key]
        pos_case_key = case_dict[col.pos_case_key]
        expect = case_dict[col.expect]
        result_key = case_dict[col.result_key]
        log.debug('\n【开始运行用例】:| {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} |'.format(
            sql_id, case_id, case_name, url, is_run, cookie, token, header_key, header_type, method, data_key, pre_case_id,
            pre_case_key, pos_case_key, expect, result_key))
        url_host = self.get_request_url_host(url, case_id)
        header_dict = self.get_header_for_dict(header_key)
        data_dict = self.get_data_for_dict(data_key)
        data_list = (sql_id, case_id, case_name, url_host, cookie, token,
                     header_dict, header_type, method, data_dict,
                     pre_case_id, pre_case_key, pos_case_key, expect, result_key)
        return data_list

    def get_request_url_host(self, url, case_id):
        if 'tqyb' in case_id:
            host_url = url
        else:
            host = path.host(case_id)
            host_url = host + url
        return host_url

    def get_header_for_dict(self, header_key):
        # headers_data = self.get_header_data(row)
        log.debug('通过header关键字获得header的dict数据')
        if header_key != None:
            try:
                request_header = op_json.get_data(path.header(), header_key)
                return request_header
            except:
                log.error('获取header_json失败')
        else:
            return None

    def get_data_for_dict(self, data_key):
        log.debug('通过data关键字获得data的dict数据')
        try:
            request_data = op_json.get_data(path.user(), data_key)
        except:
            request_data = None
        return request_data

    def get_data_change_type(self, data_dict, headers_type):
        # data_dict = self.get_data_for_dict(data_key)
        log.debug('请求进行data格式处理，header_type为："{}"'.format(headers_type))
        if headers_type == 'json' and type(data_dict) is not str:
            request_data = json.dumps(data_dict)
        elif headers_type == 'x_www_form' and type(data_dict) is not dict:
            request_data = json.loads(data_dict)
        else:
            request_data = data_dict
        return request_data


# if __name__ == '__main__':
#     get = GetDataDB()
#     case_dict = my_db.get_case_dict()
#     col_name = col.case_name
#     get.get_value_by_ColName(case_dict, col_name)